package com.sun.module_base.app

import android.app.Activity
import com.google.auto.service.AutoService
import com.hjq.toast.Toaster
import com.sun.module_base.serviceloader.IApplicationInitHandler
import com.sun.module_base.store.DataStoreUtils
import com.sun.module_base.utils.AppFrontBack
import com.sun.module_base.utils.AppFrontBackListener
import com.sun.module_network.utils.logi

/**
 *@author:sunc
 *@time :2025/7/18 16:19
 *@desc: 描述
 */
@AutoService(IApplicationInitHandler::class)
class BaseAppInitHandler : IApplicationInitHandler {

    override fun initHandler(app: BaseApp, isMainProcess: Boolean) {
        if (isMainProcess) {
            appFrontBackRegister(app)
            DataStoreUtils.init(app.applicationContext)
            Toaster.init(app)
        }
    }

    override fun onPrivacyAgreed(
        app: BaseApp,
        isMainProcess: Boolean
    ) {
    }

    override fun onPrivacyDenied(
        app: BaseApp,
        isMainProcess: Boolean
    ) {
    }

    /**
     * 注册app前后台切换监听
     */
    private fun appFrontBackRegister(app: BaseApp) {
        AppFrontBack.register(app, object : AppFrontBackListener {
            override fun onFront(activity: Activity?) {
                "app 运行在前台！".logi()
            }

            override fun onBack(activity: Activity?) {
                "app 运行在后台！".logi()
            }

        })
    }
}